package com.demo.mymusic;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.wk.myplayer.bean.WlTimeInfoBean;
import com.wk.myplayer.listener.PlayerCallback;
import com.wk.myplayer.opengl.WlGLSurfaceView;
import com.wk.myplayer.player.WlPlayer;
import com.wk.myplayer.util.WlTimeUtil;
import java.util.LinkedList;
import java.util.Locale;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {
    private WlPlayer wlPlayer;
    private TextView tvTime ,infoShow;
    private SeekBar seekBar;
    private int position;
    private boolean seek = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvTime = findViewById(R.id.tv_time);
        infoShow = findViewById(R.id.infoShow);
        seekBar = findViewById(R.id.seekbar);
        WlGLSurfaceView wlGLSurfaceView = findViewById(R.id.wlglsurfaceview);

        wlPlayer = new WlPlayer(playerCallback);
        wlPlayer.setWlGLSurfaceView(wlGLSurfaceView);


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                position = progress * wlPlayer.getDuration() / 100;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                seek = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                wlPlayer.seek(position);
                seek = false;
            }
        });

    }

    public void begin(View view) {

        //String url = Environment.getExternalStorageDirectory().getAbsolutePath() + "/动画片.mp4" ;
        String url = "http://192.168.1.109/externalLinksController/chain/%E5%8A%A8%E7%94%BB%E7%89%87.mp4?ckey=sEy8IxP11G74adi%2FodVvZixXb7Wxsub7dQgzDs4Ju6YDB8Ifzq8MrQJ5uNOuQByJ" ;
        shoMsg("播放地址: "+url);
        wlPlayer.setSource(url);
        wlPlayer.parpared();
    }

    public void pause(View view) {
        wlPlayer.pause();
    }

    public void resume(View view) {
        wlPlayer.resume();
    }


    public void stop(View view) {
        wlPlayer.stop();
    }


    public void next(View view) {
        String url = Environment.getExternalStorageDirectory().getAbsolutePath() + "/惊奇队长.mp4" ;
        shoMsg("next : "+url);
        wlPlayer.playNext(url);
    }


    private PlayerCallback playerCallback = new PlayerCallback() {
        @Override
        public void onParpared() {
            shoMsg("onParpared...");
            wlPlayer.start();
        }

        @Override
        public void onLoad(boolean load) {
            String msg = load ? "加载中...": "播放中..." ;
            shoMsg(msg);
        }

        @Override
        public void onPause(boolean pause) {
            String msg = pause ? "暂停中...": "播放中..." ;
            shoMsg(msg);
        }

        @Override
        public void onTimeInfo(WlTimeInfoBean timeInfoBean) {
            Disposable disposable= Observable.just(  timeInfoBean)
                    .subscribeOn(Schedulers.computation())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe( wlTimeInfoBean -> {

                        tvTime.setText( String.format( Locale.getDefault() ,"%s / %s" ,
                                WlTimeUtil.secdsToDateFormat( wlTimeInfoBean.getCurrentTime(), wlTimeInfoBean.getTotalTime()) ,
                                WlTimeUtil.secdsToDateFormat(wlTimeInfoBean.getTotalTime(), wlTimeInfoBean.getTotalTime())));

                        if(!seek && wlTimeInfoBean.getTotalTime() > 0)  {
                            seekBar.setProgress(wlTimeInfoBean.getCurrentTime() * 100 / wlTimeInfoBean.getTotalTime());
                        }
                    });
        }

        @Override
        public void onError(int code, String msg) {
            shoMsg("onError, code:" + code + ", msg:" + msg);
        }

        @Override
        public void onShowLog(String msg) {
            shoMsg(msg);
        }

        @Override
        public void onComplete() {
            shoMsg("onComplete");
        }

    };

    private StringBuilder sb= new StringBuilder();
    private LinkedList<String> list = new LinkedList<>();
    private void shoMsg(String msg){
        if(list.size()>=11){
            list.removeFirst();
        }
        list.add(msg) ;
        Disposable disposable= Observable.just(  list)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe( value -> {
                    sb.delete(0, sb.length()) ;
                    for(String str: value){
                        sb.append(str).append("\r\n");
                    }
                    infoShow.setText(sb.toString());
                });
    }
}
